package com.osce.ex.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csce.domain.entity.exam.ExExam;
import com.doudou.core.container.ListUtil;
import com.doudou.core.container.MapUtil;
import com.osce.common.constant.CacheConstants;
import com.osce.common.utils.DateUtils;
import com.osce.ex.mapper.ExExamMapper;
import com.osce.ex.service.tools.ExService;
import com.osce.interfaces.ex.IExExamService;
import com.osce.interfaces.ex.IExQuestionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 考试管理Service业务层处理
 *
 * @author zzx
 * @since 2023-08-09
 */
@Service
public class ExExamServiceImpl extends ServiceImpl<ExExamMapper, ExExam> implements IExExamService {

    @Resource
    private IExQuestionService questionService;
    @Resource
    private ExService exService;

    /**
     * 查询考试管理
     *
     * @param id 考试管理主键
     * @return 考试管理
     */
    @Override
    public ExExam selectExExamById(Long id) {
        return baseMapper.selectExExamById(id).setQuestionIds(baseMapper.selectExQuestionIdsById(id));
    }

    /**
     * 查询考试管理列表
     *
     * @param exExam 考试管理
     * @return 考试管理
     */
    @Override
    public List<ExExam> selectExExamList(ExExam exExam) {
        return baseMapper.selectExExamList(exExam);
    }

    /**
     * 新增考试管理
     */
    @Override
    public int insertExExam(ExExam exExam, List<Long> questionIds) {
        exExam.setCreateTime(DateUtils.getNowDate());
        int i = baseMapper.insertExExam(exExam);
        if (i > 0 && ListUtil.isNotEmpty(questionIds)) {
            baseMapper.insertExExamQuestion(exExam.getId(), questionIds);
        }
        return i;
    }


    @Override
    public int create(ExExam exExam) {
        Map<Integer, Integer> map = exService.analyseSig(exExam.getSig(), exExam.getCheckeds(), exExam.getQuestionCount());
        if (CacheConstants.OPTIONAL.equals(exExam.getSig())) {//自选
            exExam.setQuestionCount(MapUtil.valueSumInt(map));
        }
        //随机获取题目编号
        List<Long> questionIds = questionService.randomIdsByTopic(map);
        return insertExExam(exExam, questionIds);
    }

    /**
     * 修改考试管理
     *
     * @param exExam 考试管理
     * @return 结果
     */
    @Override
    public int updateExExam(ExExam exExam) {
        return baseMapper.updateExExam(exExam);
    }

    /**
     * 批量删除考试管理
     *
     * @param ids 需要删除的考试管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteExExamByIds(Long[] ids) {
        baseMapper.deleteExExamQuestionByIds(ids);
        baseMapper.deleteExExamRecordByExamId(ids);
        return baseMapper.deleteExExamByIds(ids);
    }

    /**
     * 删除考试管理信息
     *
     * @param id 考试管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteExExamById(Long id) {
        baseMapper.deleteExExamQuestionByIds(id);
        baseMapper.deleteExExamRecordByExamId(id);
        return baseMapper.deleteExExamById(id);
    }
}
